<?php
/*
 * @FilePath: \crm\application\crm\controller\auth\Role.php
 */

namespace app\crm\controller\auth;

use app\crm\controller\BaseController;
use app\crm\model\auth\AuthNode;
use app\crm\model\auth\Role as AuthRole;

class Role extends BaseController
{
    public function index()
    {
        if ($this->request->isAjax()) {
            $data = $this->request->get();
            if (count($data) >= 7) {
                $appoint = AuthRole::WhereLike('id', $data['id'] . '%')
                    ->WhereLike('auth_name', $data['auth_name'] . '%')
                    ->WhereLike('remark', $data['remark'] . '%')
                    ->WhereLike('status', $data['status'] . '%')
                    ->WhereLike('create_time', $data['create_time'] . '%')
                    ->all();
                return json(['code' => 0, 'mag' => '分页', 'count' => count($appoint), 'data' => $appoint]);
            } else {
                return json(['code' => 0, 'mag' => '分页', 'count' => count(AuthRole::all()), 'data' => AuthRole::all()]);
            }
        }
        return $this->fetch();
    }

    public function add()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $rule = [
                'auth_name|权限名称' => 'require'
            ];
            $result = $this->validate($post, $rule);
            $result !== true && $this->error($result);
            try {
                $save = (new AuthRole())->save($post);
            } catch (\Exception $e) {
                $this->error('添加失败');
            }
            $save ? $this->success('添加成功') : $this->error('添加失败');
        }
        return $this->fetch();
    }

    public function edit($id = '')
    {
        $row = AuthRole::get($id);
        empty($row) && $this->error('该权限不存在');
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $rule = [
                'auth_name|权限名称' => 'require'
            ];
            $result = $this->validate($post, $rule);
            $result !== true && $this->error($result);
            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('修改失败');
            }
            $save ? $this->success('修改成功') : $this->error('修改失败');
        }
        return $this->fetch('add', [
            'row'  => $row,
            'edit' => 'edit'
        ]);
    }

    public function delete($id = '')
    {
        $row = AuthRole::get($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isAjax()) {
            AuthRole::destroy($id);
            $this->success('删除成功');
        }
        return $this->fetch();
    }
    //节点遍历
    public function authorize($id = '')
    {
        if ($this->request->isAjax()) {
            $data = $this->request->post();
            if (isset($data['nodeIds'])) {
                $AuthRole = new AuthRole();
                if ($AuthRole->save(['node_id' => $data['nodeIds']], ['id' => $id])) {
                    return json(['msg' => '授权成功', 'code' => 0]);
                }
            } else {
                return json((new AuthNode())->AuthNodeList());
            }
        }

        $row = AuthRole::where('id', $id)->find();
        $this->assign('role', $row);
        return $this->fetch();
    }
}
